<script setup lang="ts">
import { useStore } from '../store'
import { get } from '../utils/request'
import { useRoute, useRouter } from "vue-router"
const route = useRoute()
const store = useStore()
let url = '' + route.query.url

function getQueryString(name:string) {
  const url = new URL(location.href);
  return url.searchParams.get(name);
}

if (getQueryString('code')) {
    // 拉取openid
    get(
      'https://h5.fortrip.cn/api/form/auth',
      { code: getQueryString('code') }
    ).then(res => {
      debugger
      store.auth(res.data.data)
      url = ''+ getQueryString('state')
      location.href = url.replace('$', '#')
    }).catch(() => {
      alert('微信验证失败,请稍后再试')
    })
  } else {
    // 跳转登录
    url = url.replace('#', '$')
    console.log(url);
    debugger
    location.href='https://open.weixin.qq.com/connect/oauth2/authorize?appid=wx77761385644f7677&redirect_uri=' + encodeURIComponent(location.href) + '&response_type=code&scope=snsapi_base&state=' + url + '#wechat_redirect'
  }
</script>